package com.xthh.sql_inkcute;


import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._enum.HH_EnumDeviceModelINI;
import com.xthh._enum.HH_EnumOnLineState;
import com.xthh._function.HH_CharUtil;
import com.xthh._struct.HH_T_BindDevice;
import com.xthh._struct.HH_T_DeviceOtherInfo;
import com.xthh._struct.HH_T_ESL;
import com.xthh.sql.HH_SheetName;

import java.util.ArrayList;
import java.util.List;


enum HH_EnumDevicesOtherInfoINI {
    macS,
    verS,
    hardwareVerS,
    sizeS,
    dbmI,
    deviceColorTypeI,
    widthI,
    heightI,
    batteryI,
    deviceNumberI,
    nameS, // 新增的
    timeS,// 新增的
}

enum HH_EnumESLInfoINI {
    uid,
    hexs,
}

/**
 * api查询方式
 */
public class DevicesManagerDao {

    private static final String TAG = "设备管理数据库";
    public final String SQL_SHEET_NAME_DEVICES_OTHER_INFO = "DevicesOtherInfo";
    public final String SQL_SHEET_NAME_DEVICES_MANAGER = "DevicesManager";
    public final String SQL_SHEET_NAME_ESL = "ESL";
    XTSQLiteOpenHelper_DeviceManager mOpenHelper2;//数据库的帮助类

    public DevicesManagerDao(Context context, Application api) {
        mOpenHelper2 = new XTSQLiteOpenHelper_DeviceManager(context, api);

        String strParams = "";
        strParams += (HH_EnumDevicesOtherInfoINI.macS.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.verS.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.hardwareVerS.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.sizeS.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.dbmI.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.deviceColorTypeI.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.widthI.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.heightI.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.batteryI.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.deviceNumberI.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.nameS.toString() + " varchar(64),");
        strParams += (HH_EnumDevicesOtherInfoINI.timeS.toString() + " varchar(64)");

        boolean  result = tabIsExist(SQL_SHEET_NAME_DEVICES_OTHER_INFO);
        if (SQL_SHEET_NAME_DEVICES_OTHER_INFO != null && !result){
            SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
            String sql = String.format("create table %s(%s);", SQL_SHEET_NAME_DEVICES_OTHER_INFO, strParams);
            db.execSQL(sql);
        }

        result = tabIsExist(SQL_SHEET_NAME_DEVICES_MANAGER);
        if (!result){
            SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
            String sql = String.format("create table %s(%s);", SQL_SHEET_NAME_DEVICES_MANAGER, strParams);
            db.execSQL(sql);
        }

        strParams = "";
        strParams += (HH_EnumESLInfoINI.uid.toString() + " varchar(64),");
        strParams += (HH_EnumESLInfoINI.hexs.toString() + " varchar(1024)");
        result = tabIsExist(SQL_SHEET_NAME_ESL);
        if (SQL_SHEET_NAME_ESL != null && !result){
            SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
            String sql = String.format("create table %s(%s);", SQL_SHEET_NAME_ESL, strParams);
            db.execSQL(sql);
        }
    }


    public boolean tabIsExist(String tabName){
        boolean result = false;
        if(tabName == null){
            return false;
        }
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = mOpenHelper2.getReadableDatabase();//此this是继承SQLiteOpenHelper类得到的
            String sql = "select count(*) as c from sqlite_master where type ='table' and name ='"+tabName.trim()+"' ";
            cursor = db.rawQuery(sql, null);
            if(cursor.moveToNext()){
                int count = cursor.getInt(0);
                if(count>0){
                    result = true;
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
        return result;
    }


    // 其它信息，蓝牙扫描得到的信息
    /////////////////////////////////////////////
    public static List<HH_T_DeviceOtherInfo> queryAllDeviceOtherInfo(Context ctx, Application api) {
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        return dao.queryAllDeviceOtherInfo();
    }

    // 只能是唯一的
    public static HH_T_DeviceOtherInfo queryItemDeviceOtherInfo(Context ctx, String mac, Application api) {
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        return dao.queryItemDeviceOtherInfo(mac);
    }

    // 添加和修改都使用同一个信息
    public static void addChangeOtherInfo(Context ctx, HH_T_DeviceOtherInfo tdo, Application api) {
        //Logging.i(TAG, "addChangeOtherInfo");
        if (tdo == null || tdo.getMac() == null || tdo.getMac().length() != 12)
            return;

        //Logging.i(TAG, "addChangeOtherInfo-mac=%s", tdo.getMacRes());
        HH_T_DeviceOtherInfo list = queryItemDeviceOtherInfo(ctx, tdo.getMac(), api);


        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);

        if (list == null){
            // add
            dao.insertDeviceOtherInfo(tdo);
        } else {
            // change
            dao.changeDeviceOtherInfo(tdo);
        }
    }


    private List<HH_T_DeviceOtherInfo> queryAllDeviceOtherInfo() {
        List<HH_T_DeviceOtherInfo> personList = new ArrayList<HH_T_DeviceOtherInfo>();
        SQLiteDatabase db = mOpenHelper2.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumDevicesOtherInfoINI.values().length];
            for (HH_EnumDevicesOtherInfoINI e : HH_EnumDevicesOtherInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = null; // 选择条件，给null查询所有
            String[] selectionArgs = null;//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(SQL_SHEET_NAME_DEVICES_OTHER_INFO, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_DeviceOtherInfo tr = new HH_T_DeviceOtherInfo();

                    tr.setMac(cursor.getString(HH_EnumDevicesOtherInfoINI.macS.ordinal()));
                    tr.setVer(cursor.getString(HH_EnumDevicesOtherInfoINI.verS.ordinal()));
                    tr.setHardwareVer(cursor.getString(HH_EnumDevicesOtherInfoINI.hardwareVerS.ordinal()));
                    tr.setSize(cursor.getString(HH_EnumDevicesOtherInfoINI.sizeS.ordinal()));
                    tr.setDbm(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.dbmI.ordinal())));
                    tr.setDeviceColorType(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.ordinal())));
                    tr.setWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.widthI.ordinal())));
                    tr.setHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.heightI.ordinal())));
                    tr.setBatteryB(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.batteryI.ordinal())));
                    tr.setDeviceNumber(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceNumberI.ordinal())));

                    personList.add(tr);
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return personList;
    }

    public HH_T_DeviceOtherInfo queryItemDeviceOtherInfo(String mac) {
        SQLiteDatabase db = mOpenHelper2.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumDevicesOtherInfoINI.values().length];
            for (HH_EnumDevicesOtherInfoINI e : HH_EnumDevicesOtherInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?"; // 选择条件，给null查询所有
            String[] selectionArgs = {mac};//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(SQL_SHEET_NAME_DEVICES_OTHER_INFO, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_DeviceOtherInfo tr = new HH_T_DeviceOtherInfo();
                    tr.setMac(cursor.getString(HH_EnumDevicesOtherInfoINI.macS.ordinal()));
                    tr.setVer(cursor.getString(HH_EnumDevicesOtherInfoINI.verS.ordinal()));
                    tr.setHardwareVer(cursor.getString(HH_EnumDevicesOtherInfoINI.hardwareVerS.ordinal()));
                    tr.setSize(cursor.getString(HH_EnumDevicesOtherInfoINI.sizeS.ordinal()));
                    tr.setDbm(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.dbmI.ordinal())));
                    tr.setDeviceColorType(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.ordinal())));
                    tr.setWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.widthI.ordinal())));
                    tr.setHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.heightI.ordinal())));
                    tr.setBatteryB(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.batteryI.ordinal())));
                    tr.setDeviceNumber(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceNumberI.ordinal())));

                    db.close();
                    return tr;
                }
                db.close();
            }

            db.close();
        }

        return null;
    }

    public void insertDeviceOtherInfo(HH_T_DeviceOtherInfo tdo) {
        if (tdo == null || tdo.getMac() == null || tdo.getMac().length() != 12)
            return;

        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();
            values.put(HH_EnumDevicesOtherInfoINI.macS.toString(), tdo.getMac());
            values.put(HH_EnumDevicesOtherInfoINI.verS.toString(), tdo.getVer());
            values.put(HH_EnumDevicesOtherInfoINI.hardwareVerS.toString(), tdo.getHardwareVer());
            values.put(HH_EnumDevicesOtherInfoINI.sizeS.toString(), tdo.getSize());
            values.put(HH_EnumDevicesOtherInfoINI.dbmI.toString(), String.format("%d",tdo.getDbm()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.toString(), String.format("%d",tdo.getDeviceColorType()));
            values.put(HH_EnumDevicesOtherInfoINI.widthI.toString(), String.format("%d",tdo.getWidth()));
            values.put(HH_EnumDevicesOtherInfoINI.heightI.toString(), String.format("%d",tdo.getHeight()));
            values.put(HH_EnumDevicesOtherInfoINI.batteryI.toString(), String.format("%d",tdo.getBatteryB()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceNumberI.toString(), String.format("%d",tdo.getDeviceNumber()));

            values.put(HH_EnumDevicesOtherInfoINI.nameS.toString(), tdo.getName());
            values.put(HH_EnumDevicesOtherInfoINI.timeS.toString(), tdo.getTimeLink());
            long id = db.insert(SQL_SHEET_NAME_DEVICES_OTHER_INFO, null, values);
            db.close(); //数据库关闭
        }
    }

    public void changeDeviceOtherInfo(HH_T_DeviceOtherInfo tdo) {
        if (tdo == null || tdo.getMac() == null || tdo.getMac().length() != 12)
            return;

        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumDevicesOtherInfoINI.verS.toString(), tdo.getVer());
            values.put(HH_EnumDevicesOtherInfoINI.hardwareVerS.toString(), tdo.getHardwareVer());
            values.put(HH_EnumDevicesOtherInfoINI.sizeS.toString(), tdo.getSize());
            values.put(HH_EnumDevicesOtherInfoINI.dbmI.toString(), String.format("%d",tdo.getDbm()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.toString(), String.format("%d",tdo.getDeviceColorType()));
            values.put(HH_EnumDevicesOtherInfoINI.widthI.toString(), String.format("%d",tdo.getWidth()));
            values.put(HH_EnumDevicesOtherInfoINI.heightI.toString(), String.format("%d",tdo.getHeight()));
            values.put(HH_EnumDevicesOtherInfoINI.batteryI.toString(), String.format("%d",tdo.getBatteryB()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceNumberI.toString(), String.format("%d",tdo.getDeviceNumber()));
            values.put(HH_EnumDevicesOtherInfoINI.nameS.toString(), tdo.getName());
            values.put(HH_EnumDevicesOtherInfoINI.timeS.toString(), tdo.getTimeLink());

            String whereClause = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?";
            int count = db.update(SQL_SHEET_NAME_DEVICES_OTHER_INFO, values, whereClause, new String[]{tdo.getMac()});
            db.close(); //数据库关闭
        }
    }




    /////////////////////////////////////////////////////////
    // 设备管理信息
    /////////////////////////////////////////////
    public static void deleteManagerInfo(Context ctx, Application api, String mac) {
        if (mac == null)
            return;
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        dao.deleteDeviceManagerInfo(mac);
    }

    public static List<HH_T_DeviceOtherInfo> queryAllDeviceManagerInfo(Context ctx, Application api) {
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        return dao.queryAllDeviceManagerInfo();
    }

    // 只能是唯一的
    public static HH_T_DeviceOtherInfo queryItemDeviceManagerInfo(Context ctx, String mac, Application api) {
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        return dao.queryItemDeviceManagerInfo(mac);
    }

    // 添加和修改都使用同一个信息
    public static void addChangeManagerInfo(Context ctx, HH_T_DeviceOtherInfo tdo, Application api) {
        //Logging.i(TAG, "addChangeManagerInfo");
        if (tdo == null || tdo.getMac() == null || tdo.getMac().length() != 12)
            return;

        HH_T_DeviceOtherInfo list = queryItemDeviceManagerInfo(ctx, tdo.getMac(), api);
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);

        if (list == null){
            // add
            dao.insertDeviceManagerInfo(tdo);
        } else {
            // change
            dao.changeDeviceManagerInfo(tdo);
        }
    }

    public static void ChangeNameOnDeviceManager(Application api, Context ctx, String mac, String name){
        if (mac == null || mac.length() != 12)
            return;

        if (name == null)
            return;

        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        dao.ChangeNameOnDeviceManager(mac, name);
    }

    public static void ChangeTimeOnDeviceManager(Application api, Context ctx, String mac, String time){
        if (mac == null || mac.length() != 12)
            return;

        if (time == null)
            return;

        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        dao.ChangeTimeOnDeviceManager(mac, time);
    }


    private List<HH_T_DeviceOtherInfo> queryAllDeviceManagerInfo() {
        List<HH_T_DeviceOtherInfo> personList = new ArrayList<HH_T_DeviceOtherInfo>();
        SQLiteDatabase db = mOpenHelper2.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumDevicesOtherInfoINI.values().length];
            for (HH_EnumDevicesOtherInfoINI e : HH_EnumDevicesOtherInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = null; // 选择条件，给null查询所有
            String[] selectionArgs = null;//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(SQL_SHEET_NAME_DEVICES_MANAGER, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_DeviceOtherInfo tr = new HH_T_DeviceOtherInfo();

                    tr.setMac(cursor.getString(HH_EnumDevicesOtherInfoINI.macS.ordinal()));
                    tr.setVer(cursor.getString(HH_EnumDevicesOtherInfoINI.verS.ordinal()));
                    tr.setHardwareVer(cursor.getString(HH_EnumDevicesOtherInfoINI.hardwareVerS.ordinal()));
                    tr.setSize(cursor.getString(HH_EnumDevicesOtherInfoINI.sizeS.ordinal()));
                    tr.setDbm(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.dbmI.ordinal())));
                    tr.setDeviceColorType(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.ordinal())));
                    tr.setWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.widthI.ordinal())));
                    tr.setHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.heightI.ordinal())));
                    tr.setBatteryB(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.batteryI.ordinal())));
                    tr.setDeviceNumber(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceNumberI.ordinal())));
                    tr.setName(cursor.getString(HH_EnumDevicesOtherInfoINI.nameS.ordinal()));
                    tr.setTimeLink(cursor.getString(HH_EnumDevicesOtherInfoINI.timeS.ordinal()));


                    personList.add(tr);
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return personList;
    }

    public HH_T_DeviceOtherInfo queryItemDeviceManagerInfo(String mac) {
        SQLiteDatabase db = mOpenHelper2.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumDevicesOtherInfoINI.values().length];
            for (HH_EnumDevicesOtherInfoINI e : HH_EnumDevicesOtherInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?"; // 选择条件，给null查询所有
            String[] selectionArgs = {mac};//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(SQL_SHEET_NAME_DEVICES_MANAGER, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_DeviceOtherInfo tr = new HH_T_DeviceOtherInfo();
                    tr.setMac(cursor.getString(HH_EnumDevicesOtherInfoINI.macS.ordinal()));
                    tr.setVer(cursor.getString(HH_EnumDevicesOtherInfoINI.verS.ordinal()));
                    tr.setHardwareVer(cursor.getString(HH_EnumDevicesOtherInfoINI.hardwareVerS.ordinal()));
                    tr.setSize(cursor.getString(HH_EnumDevicesOtherInfoINI.sizeS.ordinal()));
                    tr.setDbm(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.dbmI.ordinal())));
                    tr.setDeviceColorType(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.ordinal())));
                    tr.setWidth(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.widthI.ordinal())));
                    tr.setHeight(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.heightI.ordinal())));
                    tr.setBatteryB(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.batteryI.ordinal())));
                    tr.setDeviceNumber(HH_CharUtil.Atoi(cursor.getString(HH_EnumDevicesOtherInfoINI.deviceNumberI.ordinal())));
                    tr.setName(cursor.getString(HH_EnumDevicesOtherInfoINI.nameS.ordinal()));
                    tr.setTimeLink(cursor.getString(HH_EnumDevicesOtherInfoINI.timeS.ordinal()));

                    db.close();
                    return tr;
                }
                db.close();
            }

            db.close();
        }

        return null;
    }

    public void insertDeviceManagerInfo(HH_T_DeviceOtherInfo tdo) {
        if (tdo == null || tdo.getMac() == null || tdo.getMac().length() != 12)
            return;

        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();
            values.put(HH_EnumDevicesOtherInfoINI.macS.toString(), tdo.getMac());
            values.put(HH_EnumDevicesOtherInfoINI.verS.toString(), tdo.getVer());
            values.put(HH_EnumDevicesOtherInfoINI.hardwareVerS.toString(), tdo.getHardwareVer());
            values.put(HH_EnumDevicesOtherInfoINI.sizeS.toString(), tdo.getSize());
            values.put(HH_EnumDevicesOtherInfoINI.dbmI.toString(), String.format("%d",tdo.getDbm()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.toString(), String.format("%d",tdo.getDeviceColorType()));
            values.put(HH_EnumDevicesOtherInfoINI.widthI.toString(), String.format("%d",tdo.getWidth()));
            values.put(HH_EnumDevicesOtherInfoINI.heightI.toString(), String.format("%d",tdo.getHeight()));
            values.put(HH_EnumDevicesOtherInfoINI.batteryI.toString(), String.format("%d",tdo.getBatteryB()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceNumberI.toString(), String.format("%d",tdo.getDeviceNumber()));
            values.put(HH_EnumDevicesOtherInfoINI.nameS.toString(), tdo.getName());
            values.put(HH_EnumDevicesOtherInfoINI.timeS.toString(), tdo.getTimeLink());
            long id = db.insert(SQL_SHEET_NAME_DEVICES_MANAGER, null, values);
            db.close(); //数据库关闭
        }
    }

    public void changeDeviceManagerInfo(HH_T_DeviceOtherInfo tdo) {
        if (tdo == null || tdo.getMac() == null || tdo.getMac().length() != 12)
            return;

        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumDevicesOtherInfoINI.verS.toString(), tdo.getVer());
            values.put(HH_EnumDevicesOtherInfoINI.hardwareVerS.toString(), tdo.getHardwareVer());
            values.put(HH_EnumDevicesOtherInfoINI.sizeS.toString(), tdo.getSize());
            values.put(HH_EnumDevicesOtherInfoINI.dbmI.toString(), String.format("%d",tdo.getDbm()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceColorTypeI.toString(), String.format("%d",tdo.getDeviceColorType()));
            values.put(HH_EnumDevicesOtherInfoINI.widthI.toString(), String.format("%d",tdo.getWidth()));
            values.put(HH_EnumDevicesOtherInfoINI.heightI.toString(), String.format("%d",tdo.getHeight()));
            values.put(HH_EnumDevicesOtherInfoINI.batteryI.toString(), String.format("%d",tdo.getBatteryB()));
            values.put(HH_EnumDevicesOtherInfoINI.deviceNumberI.toString(), String.format("%d",tdo.getDeviceNumber()));

            values.put(HH_EnumDevicesOtherInfoINI.nameS.toString(), tdo.getName());
            values.put(HH_EnumDevicesOtherInfoINI.timeS.toString(), tdo.getTimeLink());

            String whereClause = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?";
            int count = db.update(SQL_SHEET_NAME_DEVICES_MANAGER, values, whereClause, new String[]{tdo.getMac()});
            db.close(); //数据库关闭
        }
    }



    public void ChangeNameOnDeviceManager(String mac, String name) {
        if (mac == null || mac.length() != 12)
            return;
        if (name == null)
            return;
        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumDevicesOtherInfoINI.nameS.toString(), name);

            String whereClause = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?";
            int count = db.update(SQL_SHEET_NAME_DEVICES_MANAGER, values, whereClause, new String[]{mac});
            db.close(); //数据库关闭
        }
    }


    public void ChangeTimeOnDeviceManager(String mac, String time) {
        if (mac == null || mac.length() != 12)
            return;
        if (time == null)
            return;
        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumDevicesOtherInfoINI.timeS.toString(), time);

            String whereClause = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?";
            int count = db.update(SQL_SHEET_NAME_DEVICES_MANAGER, values, whereClause, new String[]{mac});
            db.close(); //数据库关闭
        }
    }

    public void deleteDeviceManagerInfo(String mac){
        if (mac == null)
            return;
        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();//获得可写入数据库对象
        if (db.isOpen()) {
            String whereClasuse = HH_EnumDevicesOtherInfoINI.macS.toString() + " = ?";
            String[] whereArgs = {mac};
            int count = db.delete(SQL_SHEET_NAME_DEVICES_MANAGER, whereClasuse, whereArgs);
            db.close();
        }
    }




    /////////////////////////////////////////////////////////
    // ESL
    /////////////////////////////////////////////
    public static void deleteESLInfo(Context ctx, Application api, String uid) {
        if (uid == null)
            return;
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        dao.deleteESLInfo(uid);
    }

    public static List<HH_T_ESL> queryAllESLInfo(Context ctx, Application api) {
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        return dao.queryAllESLInfo();
    }

    // 只能是唯一的
    public static HH_T_ESL queryItemESLInfo(Context ctx, String uid, Application api) {
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);
        return dao.queryItemESLInfo(uid);
    }

    // 添加和修改都使用同一个信息
    public static void addChangeESLInfo(Context ctx, HH_T_ESL tdo, Application api) {
        //Logging.i(TAG, "addChangeManagerInfo");
        if (tdo == null || tdo.getUid() == null)
            return;

        HH_T_ESL list = queryItemESLInfo(ctx, tdo.getUid(), api);
        DevicesManagerDao dao = new DevicesManagerDao(ctx, api);

        if (list == null){
            // add
            dao.insertESLInfo(tdo);
        } else {
            // change
            dao.changeESLInfo(tdo);
        }
    }


    private List<HH_T_ESL> queryAllESLInfo() {
        List<HH_T_ESL> personList = new ArrayList<HH_T_ESL>();
        SQLiteDatabase db = mOpenHelper2.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumESLInfoINI.values().length];
            for (HH_EnumESLInfoINI e : HH_EnumESLInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = null; // 选择条件，给null查询所有
            String[] selectionArgs = null;//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(SQL_SHEET_NAME_ESL, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {

                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_ESL tr = new HH_T_ESL();
                    tr.setUid(cursor.getString(HH_EnumESLInfoINI.uid.ordinal()));
                    tr.setHexs(cursor.getString(HH_EnumESLInfoINI.hexs.ordinal()));
                    personList.add(tr);
                }
                db.close();
                return personList;
            }

            db.close();
        }

        return personList;
    }

    public HH_T_ESL queryItemESLInfo(String uid) {
        SQLiteDatabase db = mOpenHelper2.getReadableDatabase();
        if (db.isOpen()) {

            String[] columns = new String[HH_EnumESLInfoINI.values().length];
            for (HH_EnumESLInfoINI e : HH_EnumESLInfoINI.values()) {
                columns[e.ordinal()] = e.name();
            }

            String selection = HH_EnumESLInfoINI.uid.toString() + " = ?"; // 选择条件，给null查询所有
            String[] selectionArgs = {uid};//选择条件参数,会把选择条件中的？替换成这个数组中的值
            String groupBy = null; // 分组语句 group by name  注意些的时候需要要group by 去掉
            String having = null;  // 过滤语句
            String orderBy = null; //排序

            Cursor cursor = db.query(SQL_SHEET_NAME_ESL, columns, selection, selectionArgs, groupBy, having, orderBy);

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) { //向下移动一位，直到最后一位,不可以往下移动了，停止
                    HH_T_ESL tr = new HH_T_ESL();
                    tr.setUid(cursor.getString(HH_EnumESLInfoINI.uid.ordinal()));
                    tr.setHexs(cursor.getString(HH_EnumESLInfoINI.hexs.ordinal()));
                    db.close();
                    return tr;
                }
                db.close();
            }

            db.close();
        }

        return null;
    }

    public void insertESLInfo(HH_T_ESL tdo) {
        if (tdo == null || tdo.getUid() == null)
            return;

        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();
            values.put(HH_EnumESLInfoINI.uid.toString(), tdo.getUid());
            values.put(HH_EnumESLInfoINI.hexs.toString(), tdo.getHexs());
            long id = db.insert(SQL_SHEET_NAME_ESL, null, values);
            db.close(); //数据库关闭
        }
    }

    public void changeESLInfo(HH_T_ESL tdo) {
        if (tdo == null || tdo.getUid() == null)
            return;

        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumESLInfoINI.uid.toString(), tdo.getUid());
            values.put(HH_EnumESLInfoINI.hexs.toString(), tdo.getHexs());

            String whereClause = HH_EnumESLInfoINI.uid.toString() + " = ?";
            int count = db.update(SQL_SHEET_NAME_ESL, values, whereClause, new String[]{tdo.getUid()});
            db.close(); //数据库关闭
        }
    }



    public void ChangeNameOnESL(String uid, String hexs) {
        if (uid == null)
            return;
        if (hexs == null)
            return;
        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();
        if (db.isOpen()) {  //如果数据库打开
            ContentValues values = new ContentValues();

            values.put(HH_EnumESLInfoINI.hexs.toString(), hexs);

            String whereClause = HH_EnumESLInfoINI.uid.toString() + " = ?";
            int count = db.update(SQL_SHEET_NAME_ESL, values, whereClause, new String[]{uid});
            db.close(); //数据库关闭
        }
    }


    public void deleteESLInfo(String uid){
        if (uid == null)
            return;
        SQLiteDatabase db = mOpenHelper2.getWritableDatabase();//获得可写入数据库对象
        if (db.isOpen()) {
            String whereClasuse = HH_EnumESLInfoINI.uid.toString() + " = ?";
            String[] whereArgs = {uid};
            int count = db.delete(SQL_SHEET_NAME_ESL, whereClasuse, whereArgs);
            db.close();
        }
    }

}